package xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor;

import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.command.*;
import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.command.edit.AddStringCommand;
import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.command.edit.PasteCommand;
import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.command.edit.RemoveCommand;

/**
 * 对于备份，实际我们可能做很多备份，如文本编辑中的撤销与反撤销。
 * <p>
 * 而其中对应的操作可能有很多种，可能不同的行为，很容易想到命令模式。
 * <p>
 * 那么撤销反撤销，我们除了全使用备忘录模式，将 Memento 放入数组列表，指针移动。或者撤销栈，反撤销栈中以外。
 * <p>
 * 可以用命令模式进行反撤销，即再执行一遍操作。（相对于 Memento，这种不完全的保存，可能命令模式消耗内存更小。参考 git，就是流程式保存。）
 * <p>
 * 而在如浏览器中，网页的后退前进，甚至可以全用命令模式，都去再次访问页面，而不是对保存的页面展示。
 * <p>
 * 而编辑器这里，也可以进行反向操作，来做到 Undo 效果。
 *
 * @author xing
 * @date 2023/2/10 14:36:14
 */
public class Main {
    public static void main(String[] args) {
        Editor editor = new Editor("init");
        System.out.println("origin content: " + editor.getContent());
//         editor.getHistory().showAllUndoHis();

        // --------- add
        AddStringCommand addStringCommand = new AddStringCommand("ns");
        CommandProxy commandProxy = new CommandProxy(addStringCommand);
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();

        // ---------- paste
        PasteCommand pasteCommand = new PasteCommand(" paste ");
        commandProxy = new CommandProxy(pasteCommand);
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();

        // --------- remove
        RemoveCommand removeCommand = new RemoveCommand(1);
        commandProxy = new CommandProxy(removeCommand);
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();

        // ---------- undo
        UndoCommand undoCommand = new UndoCommand();
        commandProxy = new CommandProxy(undoCommand);
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();

        // -------- redo
        RedoCommand redoCommand = new RedoCommand();
        commandProxy = new CommandProxy(redoCommand);
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
//        editor.execute(commandProxy);
//        System.out.println("now content: " + editor.getContent());
//        // editor.getHistory().showAllUndoHis();

        // ------------- undo
        undoCommand = new UndoCommand();
        commandProxy = new CommandProxy(undoCommand);
        editor.execute(commandProxy);
        System.out.println("now content: " + editor.getContent());
        // editor.getHistory().showAllUndoHis();
    }
}
